﻿/*
    标准模板：弹出选择面板
    注意，只需要修改搜索框和mini.DataGrid相关的列配置信息即可。
*/

var rs = (function(rs) {

  //----------------------------------- 公共接口 ---------------------------------------------
  /* 客户分类 */
  rs.popSelectWindowKhfl = function(e) {
    // 初始化设置
    var win = new TreeSelectWindow({
      keyLable: '客户分类编码/名称：',
      bodyHtml: '<div class="mini-fit"><div class="mini-treegrid" id="treegridKhfl" style="height:100%;width:100%" showColumnsMenu="true" showTreeIcon="true" resultAsTree="false" treeColumn="flid" idField="flid" parentField="sjbm" dataField="data"><div property="columns"><div field="flid" headerAlign="center" name="flid">客户分类编码</div><div field="mc" headerAlign="center">客户分类名称</div></div></div></div>',
      treegridId: 'treegridKhfl', // 和bodyHtml中保持一致！
      valueField: 'flid', // 和bodyHtml中保持一致！
      textField: 'mc', // 和bodyHtml中保持一致！
      errorMsgSettings: {
        before: '客户分类',
        after: '非末级客户分类，请重新选择'
      }
    });
    win.set({
      url: './jcsj/khflQuery.do?opttype=1&mkid=MK020115',
      title: '选择客户分类',
      width: 600,
      height: 450
    });
    win.show();

    var buttonEdit = e.sender;
    win.setData(null, function(action) {
      if (action == 'ok') {
        // 返回数据
        var data = win.getData();
        if (data) {
          buttonEdit.setValue(data.value);
          buttonEdit.setText(data.text);
        }
      }
    });
  };

  /* 客户地区 */
  rs.popSelectWindowKhdq = function(e) {
    // 初始化设置
    var win = new TreeSelectWindow({
      keyLable: '客户地区编码/名称：',
      bodyHtml: '<div class="mini-fit"><div class="mini-treegrid" id="treegridKhdq" style="height:100%;width:100%" showColumnsMenu="true" showTreeIcon="true" resultAsTree="false" treeColumn="dqid" idField="dqid" parentField="fdqid" dataField="data"><div property="columns"><div field="dqid" headerAlign="center" name="dqid">客户地区编码</div><div field="dqmc" headerAlign="center">客户地区名称</div></div></div></div>',
      treegridId: 'treegridKhdq', // 和bodyHtml中保持一致！     
      valueField: 'dqid', // 和bodyHtml中保持一致！               
      textField: 'dqmc', // 和bodyHtml中保持一致！                
      errorMsgSettings: {
        before: '客户地区',
        after: '非末级客户地区，请重新选择'
      }
    });
    win.set({
      url: './jcsj/khdqQuery.do?opttype=1&mkid=MK020114',
      title: '选择客户地区',
      width: 600,
      height: 450
    });
    win.show();

    var buttonEdit = e.sender;
    win.setData(null, function(action) {
      if (action == 'ok') {
        // 返回数据
        var data = win.getData();
        if (data) {
          buttonEdit.setValue(data.value);
          buttonEdit.setText(data.text);
        }
      }
    });
  };

  /* 物料分类 */
  rs.popSelectWindowWlfl = function(e) {
    // 初始化设置
    var win = new TreeSelectWindow({
      keyLable: '物料分类编码/名称：',
      bodyHtml: '<div class="mini-fit"><div class="mini-treegrid" id="treegridWlfl" style="height:100%;width:100%" showColumnsMenu="true" showTreeIcon="true" resultAsTree="false" treeColumn="flid" idField="flid" parentField="fflid" dataField="data"><div property="columns"><div field="flid" headerAlign="center" name="flid">物料分类编码</div><div field="flmc" headerAlign="center">物料分类名称</div></div></div></div>',
      treegridId: 'treegridWlfl', // 和bodyHtml中保持一致！
      valueField: 'flid', // 和bodyHtml中保持一致！
      textField: 'flmc', // 和bodyHtml中保持一致！
      errorMsgSettings: {
        before: '物料分类',
        after: '非末级物料分类，请重新选择'
      }
    });
    win.set({
      url: './jcsj/wlflQuery.do?opttype=1&mkid=MK020109',
      title: '选择物料分类',
      width: 600,
      height: 450
    });
    win.show();

    var buttonEdit = e.sender;
    win.setData(null, function(action) {
      if (action == 'ok') {
        // 返回数据
        var data = win.getData();
        if (data) {
          buttonEdit.setValue(data.value);
          buttonEdit.setText(data.text);
        }
      }
    });
  };
  //----------------------------------- 公共接口 ---------------------------------------------

  //----------------------------------- 控件定义 ---------------------------------------------
  TreeSelectWindow = function(settings) {
    TreeSelectWindow.superclass.constructor.call(this);

    if (typeof settings === 'object') {
      this.keyLable = settings.keyLable || '选择';
      this.bodyHtml = settings.bodyHtml || '';
      this.treegridId = settings.treegridId || 'treegrid';
      this.valueField = settings.valueField;
      this.textField = settings.textField;
      this.searchFields = settings.searchFields || [this.valueField, this.textField];
      this.errorMsgSettings = settings.errorMsgSettings;
    }

    this.initControls();
    this.initEvents();
  };

  mini.extend(TreeSelectWindow, mini.Window, {

    url: '',
    keyField: 'key',
    multiSelect: false,
    showFolderCheckBox: false,
    checkRecursive: false,
    title: '选择树形节点',
    width: 600,
    height: 450,
    bodyStyle: 'padding:0;',
    allowResize: true,
    showModal: true,
    showToolbar: true,
    showFooter: true,

    initControls: function() {
      //toolbar
      var toolbarEl = this.getToolbarEl();
      var keyLable = this.keyLable || '选择';
      var topHtml =
        '<div style="padding:5px 10px;"><label for="keyText$text">' + this.keyLable + '</label><input style="margin-right: 5px;" name="keyText" id="keyText" class="mini-textbox" style="width:160px;"/><a name="searchBtn" class="mini-button">查找</a></div>';
      jQuery(toolbarEl).append(topHtml);

      // body
      var bodyEl = this.getBodyEl();
      jQuery(bodyEl).append(this.bodyHtml);

      //footer
      var footerEl = this.getFooterEl();
      var footerHtml =
        '<div style="padding:8px;text-align:center;"><a name="okBtn" class="mini-button" style="width:60px;">确定</a><span style="display:inline-block;width:25px;"></span><a name="cancelBtn" class="mini-button" style="width:60px;">取消</a></div>';
      jQuery(footerEl).append(footerHtml);

      //组件对象
      mini.parse(this.el);
      this._okBtn = mini.getbyName('okBtn', this);
      this._cancelBtn = mini.getbyName('cancelBtn', this);
      this._searchBtn = mini.getbyName('searchBtn', this);
      this._keyText = mini.getbyName('keyText', this);
      this.tree = mini.get(this.treegridId);
    },

    initEvents: function() {
      var keyText = this._keyText;
      var tree = this.tree;
      var valueField = this.valueField;
      var textField = this.textField;
      var searchFields = this.searchFields;

      function filterTree() {
        var key = keyText.getValue().trim();
        if (key === '') {
          tree.clearFilter();
        } else {
          key = key.toLowerCase();
          tree.filter(function(node) {
            for (var i = 0; i < searchFields.length; i++) {
              var field = searchFields[i];
              var value = node[field];
              if (value && value.toLowerCase().indexOf(key) >= 0) {
                return true;
              }
            }
          });
          tree.expandAll();
        }
      }
      this._searchBtn.on('click', function(e) {
        filterTree();
      }, this);
      this._keyText.on('enter', function(e) {
        filterTree();
      }, this);

      /////////////////////////////////////
      this._okBtn.on('click', function(e) {
        //不能选择父节点
        var node = this.tree.getSelectedNode();
        if (node && this.tree.isLeaf(node) == false) {
          var msg;
          if (typeof this.errorMsgSettings === 'object') {
            var o = this.errorMsgSettings;
            msg = o.before + '【' + node[valueField] + '：' + node[textField] + '】' + o.after;
          } else {
            msg = '不能选择父节点，请重新选择';
          }
          rs.alert(msg);
          return;
        }

        var ret = true;
        if (this._Callback) ret = this._Callback('ok');
        if (ret !== false) {
          this.hide();
        }
      }, this);
      this._cancelBtn.on('click', function(e) {
        var ret = true;
        if (this._Callback) ret = this._Callback('cancel');
        if (ret !== false) {
          this.hide();
        }
      }, this);
      this.on('beforebuttonclick', function(e) {
        if (e.name == 'close') {
          e.cancel = true;
          var ret = true;
          if (this._Callback) ret = this._Callback('close');
          if (ret !== false) {
            this.hide();
          }
        }
      }, this);
    },
    setMultiSelect: function(value) {
      this.multiSelect = value;
      this.tree.setShowCheckBox(value);
    },
    setShowFolderCheckBox: function(value) {
      this.showFolderCheckBox = value;
      this.tree.setShowFolderCheckBox(value);
    },
    setCheckRecursive: function(value) {
      this.checkRecursive = value;
      this.tree.setCheckRecursive(value);
    },

    setUrl: function(value) {
      this.url = value;

      this.tree.setUrl(value);
    },
    setKeyField: function(value) {
      this.keyField = value;
    },
    setData: function(data, callback) {
      this._Callback = callback;

    },
    getData: function() {
      var nodes = [];
      if (this.multiSelect == true) nodes = this.tree.getCheckedNodes();
      else {
        var node = this.tree.getSelectedNode();
        if (node) nodes.push(node);
      }

      var values = [],
        texts = [];
      for (var i = 0, l = nodes.length; i < l; i++) {
        var node = nodes[i];
        values.push(node[this.valueField]);
        texts.push(node[this.textField]);
      }
      var data = {};
      data.value = values.join(',');
      data.text = texts.join(',');
      return data;
    }
  });
  mini.regClass(TreeSelectWindow, 'TreeSelectWindow');
  //----------------------------------- 控件定义 ---------------------------------------------

  return rs;
})(window.rs || {});